﻿namespace ScrumSprintMonitor.Samples.Speech
{
	#region #using Directives

	using System;
	using System.ComponentModel.Composition;

	using Extensibility;
	using Extensibility.Events;

	using Infrastructure;

	using Microsoft.Practices.Composite.Events;

	#endregion

	[Export(typeof(ScrumSprintMonitorAddIn))]
	[AddIn(Name = "Speech")]
	public class SpeechAddIn : ScrumSprintMonitorAddIn
	{
		private SpeechService speechService;

		#region Overrides of ScrumSprintMonitorAddIn

		public override void Load()
		{
			var eventAggregator = ServiceProvider.GetService<IEventAggregator>();
			eventAggregator.GetEvent<MonitorStartedEvent>().Subscribe(OnMonitorStarted);
		}

		public override void Unload()
		{
			if (this.speechService != null)
			{
				this.speechService.Dispose();
			}
		}

		#endregion

		private void OnMonitorStarted(EventArgs obj)
		{
			var reportableData = (IReportableData) ServiceProvider.GetService(typeof(IReportableData));

			this.speechService = new SpeechService(reportableData);
		}
	}
}